<html>
<head>
<title>YUI Tests</title>
<link type="text/css" rel="stylesheet" href="../build/logger/assets/logger.css" />     
<link type="text/css" rel="stylesheet" href="../build/yuitest/assets/testlogger.css" />     

<script type="text/javascript" src="../build/yahoo/yahoo.js"></script>
<script type="text/javascript" src="../build/dom/dom.js"></script>
<script type="text/javascript" src="../build/event/event.js"></script>
<script type="text/javascript" src="../build/logger/logger.js"></script>
<script type="text/javascript" src="../build/yuitest/yuitest-beta.js"></script>

</head>
<body>
<h1>yuitest example</h1>
<p>This page contains tests being run by yuitest, the results of which<br />
are output in the logger to the right. <br />View the source to see how it's done.</p>
<p><input type="button" value="Run Tests" id="btnRun" /></p>
<script type="text/javascript">


YAHOO.namespace("example.test");

YAHOO.example.test.ArrayTestCase = new YAHOO.tool.TestCase({

    //the name of the test case - if not provided, one is automatically generated
    name: "Array Tests",
    
    //-------------------------------------------------------------------------
    // Setup and teardown
    //-------------------------------------------------------------------------

    /*
     * The setUp() method is used to setup data that necessary for a test to
     * run. This method is called immediately before each test method is run,
     * so it is run as many times as there are test methods.
     */
    setUp : function () {
    
        this.data = new Array (0,1,2,3,4,5);
    
    },
    
    /*
     * The tearDown() method is used to clean up the initialization that was done
     * in the setUp() method. Ideally, it should free up all memory allocated in
     * setUp(), anticipating any possible changes to the data. This method is called
     * immediately after each test method is run.
     */
    tearDown : function () {
        delete this.data;
    },
    
    //-------------------------------------------------------------------------
    // Basic tests - all method names must begin with "test"
    //-------------------------------------------------------------------------
    
    testPush : function() {
    
        //do whatever data manipulation is necessary
        this.data.push(6);
    
        //basic equality assertion - expected value, actual value, optional error message
        YAHOO.util.Assert.areEqual(7, this.data.length, "Array should have seven items.");
        YAHOO.util.Assert.areEqual(6, this.data[6], "Value should be 6.");
        
        //array-specific assertions
        YAHOO.util.ArrayAssert.contains(6, this.data, "Array should contain 7.");
        
        //these two assertions are the same because we're dealing with primitives
        YAHOO.util.ArrayAssert.itemsAreEqual([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");
        YAHOO.util.ArrayAssert.itemsAreSame([0,1,2,3,4,5,6], this.data, "Arrays should be equal.");        
        
    },
    
    testPop : function() {
    
        //do whatever data manipulation is necessary
        var value = this.data.pop();
    
        //basic equality assertion - expected value, actual value, optional error message
        YAHOO.util.Assert.areEqual(5, this.data.length, "Array should have 5 items.");
        YAHOO.util.Assert.areEqual(5, value, "Value should be 5.");   
        
    }

});

YAHOO.example.test.AdvancedOptionsTestCase = new YAHOO.tool.TestCase({

    //the name of the test case - if not provided, one is automatically generated
    name: "Advanced Options Tests",
    
    /*
     * Specifies tests that "should" be doing something other than the expected.
     */
    _should: {
    
        /*
         * Tests listed in here should fail, meaning that if they fail, the test
         * has passed. This is used mostly for YuiTest to test itself, but may
         * be helpful in other cases.
         */
        fail: {
        
            //the test named "testFail" should fail
            testFail: true
        
        },
        
        /*
         * Tests listed here should throw an error of some sort. If they throw an
         * error, then they are considered to have passed.
         */
        error: {
        
            /*
             * You can specify "true" for each test, in which case any error will
             * cause the test to pass.
             */
            testGenericError: true,
            
            /*
             * You can specify an error message, in which case the test passes only
             * if the error thrown matches the given message.
             */
            testStringError: "I'm a specific error message.",
            testStringError2: "I'm a specific error message.",
            
            /*
             * You can also specify an error object, in which case the test passes only
             * if the error thrown is on the same type and has the same message.
             */
            testObjectError: new TypeError("Number expected."),            
            testObjectError2: new TypeError("Number expected."),
            testObjectError3: new TypeError("Number expected.")
        
        },
        
        /*
         * Tests listed here should be ignored when the test case is run. For these tests,
         * setUp() and tearDown() are not called.
         */
        ignore : {
        
            testIgnore: true
            
        }    
    },
    
    //-------------------------------------------------------------------------
    // Basic tests - all method names must begin with "test"
    //-------------------------------------------------------------------------
    
    testFail : function() {
    
        //force a failure - but since this test "should" fail, it will pass
        YAHOO.util.Assert.fail("Something bad happened.");
        
    },
    
    testGenericError : function() {    
        throw new Error("Generic error");        
    },
    
    testStringError : function() {
        
        //throw a specific error message - this will pass because it "should" happen
        throw new Error("I'm a specific error message.");    
    },
    
    testStringError2 : function() {
        
        //throw a specific error message - this will fail because the message isn't expected
        throw new Error("I'm a specific error message, but a wrong one.");    
    },
    
    testObjectError : function() {
        
        //throw a specific error and message - this will pass because it "should" happen
        throw new TypeError("Number expected.");    
    },
    
    testObjectError2 : function() {
        
        //throw a specific error and message - this will fail because the type doesn't match
        throw new Error("Number expected."); 
    },
    
    testObjectError3 : function() {
        
        //throw a specific error and message - this will fail because the message doesn't match
        throw new TypeError("String expected.");    
    },
    
    testIgnore : function () {
        alert("You'll never see this.");
    }
   
});

YAHOO.example.test.ExampleSuite = new YAHOO.tool.TestSuite("YuiTest Examples");
YAHOO.example.test.ExampleSuite.add(YAHOO.example.test.ArrayTestCase);
YAHOO.example.test.ExampleSuite.add(YAHOO.example.test.AdvancedOptionsTestCase);
     
YAHOO.util.Event.onDOMReady(function (){
    //create the logger
    var logger = new YAHOO.tool.TestLogger();
    YAHOO.tool.TestRunner.add(YAHOO.example.test.ExampleSuite);
    
    //add event handler
    YAHOO.util.Event.addListener("btnRun", "click", function() {
            YAHOO.tool.TestRunner.run();
        });

    YAHOO.tool.TestRunner.run();
});

</script>
</body>
</html>
